package com.tj.jinzao.widgets;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.InputFilter;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.PublicDailyActivity;
import com.tj.jinzao.utils.ToastUtils;

/**
 * 评论框
 * Created by wjy on 2017/7/11 0011.
 */

public class CommentDialog extends Dialog implements View.OnClickListener {

    private EditText mEditView;
    private String mHint;
    private int mId = -1;
    private Context context;

    public CommentDialog(@NonNull Context context) {
        super(context, R.style.Dialog);
        this.context=context;
    }

    public CommentDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.Dialog);
        this.context=context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_reply);
        initLayoutParams();
        findViewById(R.id.tv_send).setOnClickListener(this);
        mEditView = findViewById(R.id.edt_reply);
        mEditView.setHint(mHint);
        mEditView.setText("");
        mEditView.setFocusable(true);
        mEditView.setFocusableInTouchMode(true);
        mEditView.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        mEditView.setFilters(new InputFilter[]{new MaxTextLengthFilter(200,context,"评论内容限200字以内")});

    }

    public void setHint(String hint, int id) {
        mHint = hint;
        mId = id;
        if (mEditView != null) {
            mEditView.setText("");
            mEditView.setHint(mHint);
        }
    }

    private void initLayoutParams() {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        lp.width = dm.widthPixels;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        window.setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        String content = mEditView.getText().toString().trim();
        if(content==null||content.equals("")){
            ToastUtils.showToast(context,"请输入内容");
            return;
        }
        if (mListener != null) mListener.send(content, mId);
        dismiss();
    }

    private OnSendListener mListener;

    public void setSendListener(OnSendListener listener) {
        this.mListener = listener;
    }

    public interface OnSendListener {
        void send(String content, int id);
    }

}
